package org.bp.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 时间工具类
 * 
 * @author baiping
 * @version 1.0
 * @date 2017/9/11
 */
public class DateUtils {
	public static final int HOUR = 0;
	public static final int DAY = 1;
	public static final int WEEK = 2;
	public static final int MONTH = 3;
	public static final int SEASON = 4;
	public static final int HALF_YEAR = 5;
	public static final int YEAR = 6;
	public static final String YYYYMMDDHHMMSSSSS = "yyyy-MM-dd HH:mm:ss,SSS";

	public static String formatDate(Date date) {
		return getDateFormat(YYYYMMDDHHMMSSSSS).format(date);
	}

	public static Date StrToDate(String str) {
		Date date = null;
		try {
			date = getDateFormat(YYYYMMDDHHMMSSSSS).parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	/*
	 * 毫秒转化时分秒毫秒
	 */
	public static String formatTime(Long ms) {
		Integer ss = 1000;
		Integer mi = ss * 60;
		Integer hh = mi * 60;

		Long hour = (ms) / hh;
		Long minute = (ms - -hour * hh) / mi;
		Long second = (ms - -hour * hh - minute * mi) / ss;
		Long milliSecond = ms - -hour * hh - minute * mi - second * ss;

		StringBuffer sb = new StringBuffer();
		sb.append(hour > 0 ? hour : "00").append(":");
		sb.append(minute > 0 ? minute : "00").append(":");
		sb.append(second > 0 ? second : "00").append(",");
		sb.append(milliSecond > 0 ? milliSecond : "");
		return sb.toString();
	}

	/**
	 * 将指定时间范围的日期全部统计出来
	 *
	 * @param beginDate
	 *            开始日期
	 * @param endDate
	 *            结束日期
	 */
	public static List<Date> calculateDateList(Date beginDate, Date endDate) {
		Calendar beginCal = Calendar.getInstance();
		Calendar endCal = Calendar.getInstance();
		beginCal.setTime(beginDate);
		endCal.setTime(endDate);

		List<Date> dateList = new ArrayList<Date>();
		do {
			dateList.add(beginCal.getTime());
			beginCal.add(Calendar.DAY_OF_YEAR, 1);
			beginCal.getTime();
		} while (!DateUtils.before(beginCal, endCal, DAY));
		return dateList;
	}

	/**
	 * 判断指定日期的先后顺序
	 *
	 * @param first
	 *            第一个日期
	 * @param second
	 *            第二个日期
	 * @param type
	 *            粒度
	 */
	public static boolean before(Calendar first, Calendar second, int type) {
		if (type == DAY) {
			return first.get(Calendar.YEAR) > second.get(Calendar.YEAR)
					|| (first.get(Calendar.YEAR) == second.get(Calendar.YEAR)
							&& first.get(Calendar.DAY_OF_YEAR) > second.get(Calendar.DAY_OF_YEAR));
		} else if (type == MONTH) {
			return first.get(Calendar.YEAR) > second.get(Calendar.YEAR)
					|| (first.get(Calendar.YEAR) == second.get(Calendar.YEAR)
							&& first.get(Calendar.MONTH) > second.get(Calendar.MONTH));
		} else if (type == YEAR) {
			return first.get(Calendar.YEAR) > second.get(Calendar.YEAR);
		} else {
			return false;
		}
	}

	private static final DateFormat getDateFormat(String pattern){
		 final ThreadLocal<DateFormat> df = new ThreadLocal<DateFormat>() {
			@Override
			protected DateFormat initialValue() {
				return new SimpleDateFormat(pattern);
		}};
		return df.get();
	}

	public static void main(String[] args) {
		System.out.println(formatTime(120343L));
	}
}
